<?php

namespace app\admin\model;

use think\Db;
use think\Model;

class NodeDetail extends Model
{

    // 表名
    protected $name = 'node_detail';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    protected $append = [
        'io_type_name',
        'io_category_name',
        'io_category_unit',
    ];

    // 获取因子
    public function getFactorAttr($value, $data)
    {
        return Db::name('io_category')->where('id', $data['io_category_id'])->value('factor');
    }

    public function node()
    {
        return $this->belongsTo('Node');
    }

    public function getIoTypeNameAttr($value, $data)
    {
        return Db::name('io_category')->where('id', $data['io_type_id'])->value('name');
    }

    public function getIoCategoryNameAttr($value, $data)
    {
        return Db::name('io_category')->where('id', $data['io_category_id'])->value('name');
    }

    public function getIoCategoryUnitAttr($value, $data)
    {
        return Db::name('io_category')->where('id', $data['io_type_id'])->value('unit');
    }

    public function getIoValueAttr($value, $data)
    {
        if (empty($value)) return $value;
        return rtrim(rtrim($value, '0'), '.');
    }
}
